﻿@(
    Html.Kendo().Grid<OA.Models.Menu>()
    .Name("grid")
    .HtmlAttributes(new { style = "height:99%" })
    .Filterable()
    .Pageable()
    .Selectable()
    .Scrollable()
    .ToolBar(
        toolbar => {
            toolbar.Template(
               @<button class='k-button k-button-icontext k-grid-add' onclick="openInsertWindow()">
                    <span class="k-icon k-i-add"></span>新增
                </button>
                );
        }
    )
    .Columns(
        columns =>
        {
            columns.Bound(c => c.Name);
            columns.Bound(c => c.Sort);
            columns.Command(c =>
            {
                c.Custom("编辑").Click("openEditWindow");
                c.Custom("删除").Click("deleteEntity");
                //c.Destroy();
            }).Width(190);
        }
    )
    .DataSource(
        dataSource => dataSource
            .Ajax()
            .ServerOperation(true)
            .PageSize(18)
            .Model(
                model =>
                {
                    model.Id(p => p.Id);
                }
            )
            .Read(read => read.Action("Read", "Menu"))
    //.Events(events => events.Error("error_handler"))
    )
        )

<script>
    function openInsertWindow() {
        openWindow('新建菜单', '/Menu/viewUpdate')
    }
    function openEditWindow(e) {
        e.preventDefault();
        var data = this.dataItem($(e.currentTarget).closest("tr"));
        var param = { Id: data.Id };
        openWindow('编辑账户', '/Menu/viewUpdate', param);
    }
    function deleteEntity(e) {
        e.preventDefault();
        var data = this.dataItem($(e.currentTarget).closest("tr"));

        kendo.confirm("您确定要删除该对象吗?").then(function () {
            $.post(
                '/Menu/Delete',
                { Id: data.Id },
                function (response) {
                    if (response.IsSuccess) {
                        $("#grid").data("kendoGrid").dataSource.read();
                    }
                    alert(response.Message);
                },
                "json");
        });
    }
</script>